/************************************************************************/
/*サンプル１用*/
/************************************************************************/
var directionDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
var lisKyori = [];
var lisJikan = [];
var timerIdDisp;
var timerId = [];
var rockNum=0;
var lisTest = [];

function init() {
  //document.getElementById("start").value = "";
  //document.getElementById("end").value = "";
  document.getElementById("kyori").innerHTML = "";
  document.getElementById("jikan").innerHTML = "";
    for(j=0;j<30;j++){
        document.getElementById("kyori"+j).value = "";
        document.getElementById("jikan"+j).value = "";
        //aa+=lisKyori[j];
    }
}
/*
function initialize() {
  directionsDisplay = new google.maps.DirectionsRenderer();
  //var center_point = new google.maps.LatLng(41.850033, -87.6500523);
  var center_point = new google.maps.LatLng(35.649825, 139.911318);
  var myOptions = {
    zoom:18,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    center: center_point
  }
  map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
  directionsDisplay.setMap(map);
  directionsDisplay.setPanel(document.getElementById("directionsPanel"));
}
*/
function calcRoute() {
  var start = document.getElementById("start").value;
  var end = document.getElementById("end").value;
  var request = {
    origin:start,
    destination:end,
    travelMode: google.maps.TravelMode.DRIVING
  };
  directionsService.route(request, function(result, status) {
    if (status == google.maps.DirectionsStatus.OK) {
      /*directionsDisplay.setDirections(result);*/
      computeTotalDistance(result);
    }
  });
}

function computeTotalDistance(result) {
  var kyori = 0;
  var jikan = 0;
  
  var myroute = result.routes[0];
  for (i = 0; i < myroute.legs.length; i++) {
    kyori += myroute.legs[i].distance.value;
    jikan += myroute.legs[i].duration.value;
  }
  kyori = kyori / 1000; /*km*/
  jikan = jikan / 60 ;   /*min*/
  
  document.getElementById("kyori").innerHTML = kyori + " km";
  document.getElementById("jikan").innerHTML = jikan + " min";
}


    function test() {
        //window.alert("テスト");
        
    }
    
    
    function calcRoute2() {
        var s=0;
        //10件リクエスト
        timerId["reqN0"]=setInterval("reqN(0,10)",1);
        timerId["reqN10"]=setInterval("reqN(10,10)",9000);
        timerId["reqN20"]=setInterval("reqN(20,10)",19000);
        
        //1,11,21[OK]
        //1,10,20[OK]
        //1,9,19[NG:2]
        
        
        //11件以上は0.1秒毎にリクエスト
/*
        for (k = 0; k < 30 ;k++) {
            timerId["reqN"+k]= setInterval("reqN("+k+",1)",s);
            s+=1000;
        }
*/
        //表示
        timerIdDisp=setInterval("disp()",20000);

    }

    function reqN(startRow,cntRows) {
        //wait解除
        clearTimeout(timerId["reqN"+startRow]);
        
        //指定数分リクエストを投げる
        var start = document.getElementById("start").value;
        for(i=startRow;i<startRow+cntRows;i++){
            var end = document.getElementById("from"+i).value;
            var request = {
                origin:start,
                destination:end,
                travelMode: google.maps.TravelMode.DRIVING
            };
            directionsService.route(request, 
                function(result, status) {
                    if (status == google.maps.DirectionsStatus.OK) {
                        lisKyori.push(result.routes[0].legs[0].distance.text);
                        lisJikan.push(result.routes[0].legs[0].duration.text);
                        //document.getElementById("kyori"+i).value = result.routes[0].legs[0].distance.text;
                        //document.getElementById("jikan"+i).value = result.routes[0].legs[0].duration.text;
                    }else{
                        //lisKyori.push(status);
                    }
                }
            );    
        }
    }


function disp() {
    clearTimeout(timerIdDisp);
    for(j=0;j<30;j++){
        document.getElementById("kyori"+j).value = lisKyori[j];
        document.getElementById("jikan"+j).value = lisJikan[j];
    }
}


/************************************************************************/
/*サンプル２用*/
/************************************************************************/
/*
var origin1 = "千葉県千葉市花見川区幕張町5-417-71";
var destinationA = "千葉県野田市船形1646-1";
var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix(
  {
    origins: [origin1],
    destinations: [destinationA],
    travelMode: google.maps.TravelMode.DRIVING,
    avoidHighways: false,
    avoidTolls: false
  }, callback);

function callback(response, status) {
    if (status == google.maps.DistanceMatrixStatus.OK) {
        var origins = response.originAddresses;
        var destinations = response.destinationAddresses;

        for (var i = 0; i < origins.length; i++) {
            var results = response.rows[i].elements;
            for (var j = 0; j < results.length; j++) {
                var element = results[j];
                var distance = element.distance.text;//距離
                var duration = element.duration.text;//時間
                var from = origins[i];
                var to = destinations[j];
            }
        }
    }else{alert("Status NG");}
}
*/